package com.cskaoyan.service;

import com.cskaoyan.bean.bo.WxCollectAddorDeleteBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketCollect;
import com.cskaoyan.bean.po.MarketCollectExample;
import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.vo.wxcollect.WxCollectListVO;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wei
 * @since 2022/09/08 14:49
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public CommonData queryCollectList(Byte type, int page, int limit) {
        PageHelper.startPage(page, limit);
//        MarketCollectExample marketCollectExample = new MarketCollectExample();
//        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("info");
        Integer userId = user.getId();

        /*Integer[] ids = collectMapper.selectByTypeId(type,userId);
        List<WxCollectListVO> wxCollectListVOS = new ArrayList<>();
        for (Integer id : ids) {
            MarketCollect marketCollect = collectMapper.selectByPrimaryKey(id);
            WxCollectListVO wxCollectListVO = new WxCollectListVO();
            BeanUtils.copyProperties(marketCollect, wxCollectListVO);
            String brief=goodsMapper.selectBriefById();

            wxCollectListVO.setBrief();
            wxCollectListVO.setName();
            wxCollectListVO.setPicUrl();
            wxCollectListVO.setRetailPrice();
            wxCollectListVOS.add(wxCollectListVO);
        }*/

        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.setOrderByClause("update_time desc");

        marketCollectExample.createCriteria().andTypeEqualTo(type).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);

        LinkedList<WxCollectListVO> wxCollectListVOS = new LinkedList<>();
        for (MarketCollect marketCollect : marketCollects) {

            WxCollectListVO wxCollectListVO = new WxCollectListVO();

            Integer valueId = marketCollect.getValueId();
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(valueId);

            BeanUtils.copyProperties(marketGoods, wxCollectListVO);
            BeanUtils.copyProperties(marketCollect, wxCollectListVO);

            wxCollectListVOS.add(wxCollectListVO);
        }
//        int length = ids.length;
//        int pages = length / limit+1;
        PageInfo<WxCollectListVO> wxCollectListVOPageInfo = new PageInfo<>(wxCollectListVOS);

        int pages = wxCollectListVOPageInfo.getPages();
        long total = wxCollectListVOPageInfo.getTotal();

        CommonData<WxCollectListVO> objectCommonData = new CommonData<>();
        objectCommonData.setList(wxCollectListVOS);
        objectCommonData.setLimit(limit);
        objectCommonData.setPages(pages);
        objectCommonData.setTotal((int) total);
        objectCommonData.setPage(page);
        return objectCommonData;
    }

    @Override
    public void addordelete(WxCollectAddorDeleteBO wxCollectAddorDeleteBO) {
        Byte type = wxCollectAddorDeleteBO.getType();
        Integer valueId = wxCollectAddorDeleteBO.getValueId();

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("info");
        Integer userId = user.getId();

        //判断收藏是否存在
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.createCriteria().andUserIdEqualTo(userId).andValueIdEqualTo(valueId).andDeletedEqualTo(false);

        List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);
        if (marketCollects.size() != 1) {

            MarketCollect marketCollect = new MarketCollect();
            marketCollect.setUserId(userId);
            marketCollect.setValueId(valueId);
            marketCollect.setType(type);
            marketCollect.setAddTime(new Date(System.currentTimeMillis()));
            marketCollect.setUpdateTime(new Date(System.currentTimeMillis()));

            collectMapper.insertSelective(marketCollect);
        } else {
            //删除收藏
            MarketCollectExample marketCollectExample = new MarketCollectExample();
            marketCollectExample.createCriteria().andValueIdEqualTo(valueId).andUserIdEqualTo(userId);

            collectMapper.deleteByExample(marketCollectExample);
        }


    }
}

